package com.starsoft.smdc.crawler.netease;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.starsoft.frame.util.MapUtil;
import com.starsoft.smdc.crawler.common.CrawlerService;


public class NeteaseService extends CrawlerService{
	
	private static Logger logger = LoggerFactory.getLogger(NeteaseService.class);
	
	private final String NETEASE="netease";
	
	@SuppressWarnings("unchecked")
	protected List<Map<String, Object>> parseToList(String response) {
		List<Map<String, Object>> listValue = null;
		int beginIndex = response.indexOf("(") + 1;
		int endIndex = response.lastIndexOf(")");
		String subRespose = response.substring(beginIndex, endIndex);
		ObjectMapper obMapper = new ObjectMapper();
		try {
			Map<String, Map<String, Object>> jsonMap = obMapper.readValue(subRespose, Map.class);
			listValue = MapUtil.getValueList(jsonMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listValue;
	}
	
	protected String convertSecId(String secId, boolean isToLocal){
		if(isToLocal){
			String ticker = secId.substring(1);
			String exchange = secId.substring(0,1);
			exchange = flexsetService.codeConvertFrom(SMDC_EXCHANGE, exchange, NETEASE);
			return ticker+"."+exchange;
		}else{
			int index=secId.indexOf(".");
			String ticker = secId.substring(0,index);
			String exchange = secId.substring(index+1);
			exchange = flexsetService.codeConvertTo(SMDC_EXCHANGE, exchange, NETEASE);
			return exchange+ticker;
		}
	}

	@Override
	protected String getTargetSystem() {
		return NETEASE;
	}

}
